package com.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.entity.ErrorInfo;
import com.entity.GlInterfaceData;
import com.entity.ITranData;
import com.entity.InfoData;
import com.mapper.IGlInterfaceMapper;
import com.mapper.ITranDataMapper;
import com.service.IBatchCompareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BatchCompareServiceImpl implements IBatchCompareService {

    @Autowired
    ITranDataMapper iTranDataMapper ;

    @Autowired
    IGlInterfaceMapper iGlInterfaceMapper ;


    @Override
    public void compareInfo(Map<String, Object> map) {

        List<ITranData> iTranDataList =iTranDataMapper.queryByBatNo(map);
        //异常数据
        List<ErrorInfo> errorInfoList=new ArrayList<>();
        //比对一致数据
        List<InfoData> equalInfoList=new ArrayList<>();
        //比对不一致数据
        List<InfoData> differentInfoList=new ArrayList<>();
        //异常信息
        ErrorInfo errorInfo;
        InfoData  infoData;
        Map<String,String> mapTwo;
        for (ITranData data:iTranDataList){
            mapTwo=new HashMap<>();
            mapTwo.put("busy_no",data.getBusinessCode());
            mapTwo.put("risk_code",data.getBusiProdCode());
            GlInterfaceData glDate;
            try {
                glDate = iGlInterfaceMapper.queryBusinessCodeAndRiskCode(mapTwo);
                infoData=new InfoData();
                //业务号
                infoData.setBusinessCode(data.getBusinessCode());
                infoData.setRiskCode(data.getBusiProdCode());
                //组装比对不一致的数据
                boolean  differentFlag=queryDifferentParamInfo(infoData,data,glDate);
                if (differentFlag){
                    //存储比对有差异的数据
                    differentInfoList.add(infoData);
                }else{
                    //组装比对一致的数据
                    queryParamInfo(infoData,data,glDate);
                    //存储比对相同的数据
                    equalInfoList.add(infoData);
                }

            } catch (Exception e) {
                errorInfo=new ErrorInfo();
                errorInfo.setBusinessCode(data.getBusinessCode());
                errorInfo.setPolicyCode(data.getPolicyCode());
                errorInfo.setRiskCode(data.getBusiProdCode());
                errorInfoList.add(errorInfo);
            }
        }
        String filename = "D:\\比对.xlsx";
        //指定文件
        ExcelWriter excelWriter = EasyExcel.write(filename, InfoData.class).build();

        if(errorInfoList.size()>0){
             String errorFilename = "D:\\error.xlsx";
            //调用easyexcel里面的方法实现写操作write方法两个参数，第一个参数文件路径名，第二个参数实体类class
            EasyExcel.write(errorFilename, ErrorInfo.class).sheet("error").doWrite(errorInfoList);
        }

        if(equalInfoList.size()>0){
           //每次都要创建writeSheet 这里注意必须指定sheetNo
            WriteSheet writeSheet = EasyExcel.writerSheet(0, "equal").build();
            excelWriter.write(equalInfoList, writeSheet);
        }
        if(differentInfoList.size()>0){
            //每次都要创建writeSheet 这里注意必须指定sheetNo
            WriteSheet writeSheet = EasyExcel.writerSheet(1, "different").build();
            excelWriter.write(differentInfoList, writeSheet);
        }
        //关闭流
        excelWriter.finish();
    }

    /*
     * 组装比对不一致的数据
     */
    private boolean queryDifferentParamInfo(InfoData infoData, ITranData data, GlInterfaceData glDate) {
        boolean differentFlag=false ;
        //金额
        if (data.getFeeAmount().compareTo(glDate.getAccsum())!=0){
            infoData.setFeeAmount(data.getFeeAmount().toString());
            infoData.setI14feeAmount(glDate.getAccsum().toString());
            differentFlag=true ;
        }
        //险种编码
        if(!data.getBusiProdCode().equals(glDate.getRiskcode())){
            infoData.setBusiProdCode(data.getBusiProdCode());
            infoData.setI14busiProdCode(glDate.getRiskcode());
            differentFlag=true ;
        }
        //保单号
        if(!data.getPolicyCode().equals(glDate.getContno())){
            infoData.setPolicyCode(data.getPolicyCode());
            infoData.setI14policyCode(glDate.getContno());
            differentFlag=true ;
        }
        //成本中心
        if(!data.getKostcode().equals(glDate.getKostcode())){
            infoData.setKostcode(data.getKostcode());
            infoData.setI14kostcode(glDate.getKostcode());
            differentFlag=true ;
        }
        //提数模块
        if(!data.getOpermodel().equals(glDate.getOpermodel())){
            infoData.setOpermodel(data.getOpermodel());
            infoData.setI14opermodel(glDate.getOpermodel());
            differentFlag=true ;
        }
        //管理机构
        if(!data.getManagecom().equals(glDate.getManagecom())){
            infoData.setManagecom(data.getManagecom());
            infoData.setI14managecom(glDate.getManagecom());
            differentFlag=true ;
        }
        //首续期
        if(!data.getPayprd().equals(glDate.getPayprd())){
            infoData.setPayprd(data.getPayprd());
            infoData.setI14payprd(glDate.getPayprd());
            differentFlag=true ;
        }
        return differentFlag;
    }


    /*
    * 组装比对一致的数据
    */
    private void queryParamInfo(InfoData infoData, ITranData data,GlInterfaceData glDate) {

        //金额
        infoData.setFeeAmount(data.getFeeAmount().toString());
        infoData.setI14feeAmount(glDate.getAccsum().toString());

        //险种编码
        infoData.setBusiProdCode(data.getBusiProdCode());
        infoData.setI14busiProdCode(glDate.getRiskcode());

        //保单号
        infoData.setPolicyCode(data.getPolicyCode());
        infoData.setI14policyCode(glDate.getContno());

        //成本中心
        infoData.setKostcode(data.getKostcode());
        infoData.setI14kostcode(glDate.getKostcode());

        //提数模块
        infoData.setOpermodel(data.getOpermodel());
        infoData.setI14opermodel(glDate.getOpermodel());

        //管理机构
        infoData.setManagecom(data.getManagecom());
        infoData.setI14managecom(glDate.getManagecom());

        //首续期
        infoData.setPayprd(data.getPayprd());
        infoData.setI14payprd(glDate.getPayprd());

    }
}
